/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui;

import com.logical.allhit.Kernel;
import com.logical.allhit.gui.action.AbountAction;
import com.logical.allhit.gui.action.AccountSettingAction;
import com.logical.allhit.gui.action.ClearLogAction;
import com.logical.allhit.gui.action.ExitAction;
import com.logical.allhit.gui.action.ExprotCSVAction;
import com.logical.allhit.gui.action.ProxySettingAction;
import com.logical.allhit.gui.action.SelectAllAction;
import com.logical.allhit.gui.action.SettingAction;
import com.logical.allhit.store.StorageManager;
import com.logical.allhit.util.MsgLogger;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author Sanjeet Jha
 */
public class MainView implements Runnable {
    
    private JFrame mainWindow;
    private JMenuBar menuBar;
    private static final MainView instance = new MainView();
    private MainFrom mainFrom;
    ResourceBundle bundle =
            ResourceBundle.getBundle("com/logical/allhit/resouer/menu");
    
    private MainView() {
        try {
//            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            this.mainWindow = Kernel.getInstance().getFrame();
        } catch (Exception ex) {
            Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, "mainWindow not fount...", ex);
        }
    }
    
    public static MainView getInstance() throws Exception {
        return instance;
    }
    
    @Override
    public void run() {
        mainWindow.getContentPane().removeAll();
        mainFrom = new MainFrom();
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        mainWindow.setSize(dimension.width, dimension.height - 100);
        mainWindow.setLocation(0, 0);
        mainWindow.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        mainWindow.setJMenuBar(buildMenuBar());
        mainWindow.setContentPane(mainFrom);
        MsgLogger.getInstance().setMainFrom(mainFrom);
    }
    
    private JMenuBar buildMenuBar() {
        menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu(bundle.getString("file.name"));
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenu);
        JMenuItem selectAll = new JMenuItem(new SelectAllAction(mainFrom));
        fileMenu.add(selectAll);
        JMenuItem clearLog = new JMenuItem(new ClearLogAction());
        fileMenu.add(clearLog);
        JMenuItem csv = new JMenuItem(new ExprotCSVAction());
        fileMenu.add(csv);
        JMenuItem exit = new JMenuItem(new ExitAction());
        fileMenu.add(exit);
        JMenu optionMenu = new JMenu(bundle.getString("option.name"));
        optionMenu.setMnemonic(KeyEvent.VK_O);
        menuBar.add(optionMenu);
        JMenuItem proxy = new JMenuItem(new ProxySettingAction());
        optionMenu.add(proxy);
        JMenuItem account = new JMenuItem(new AccountSettingAction());
        optionMenu.add(account);
        JMenuItem setting = new JMenuItem(new SettingAction());
        optionMenu.add(setting);
        JMenu helpMenu = new JMenu(bundle.getString("help.name"));
        helpMenu.setMnemonic(KeyEvent.VK_H);
        menuBar.add(helpMenu);
        JMenuItem about = new JMenuItem(new AbountAction());
        helpMenu.add(about);
        menuBar.add(addThemesMenu());
        return menuBar;
    }
    
    private JMenu addThemesMenu() {
        JMenu menuTheme = new javax.swing.JMenu();
        menuTheme.setText("Look & Feel (Theme)");
        menuTheme.setMnemonic('L');
        menuTheme.setName("menuTheme"); // NOI18N

        try {
            final javax.swing.UIManager.LookAndFeelInfo[] lfinfo = javax.swing.UIManager.getInstalledLookAndFeels();
            for (int i = 0; i < lfinfo.length; ++i) {
                JMenuItem miTheme2 = new JMenuItem();
                miTheme2.setText(lfinfo[i].getName());
                miTheme2.setName(lfinfo[i].getName());
                final int j = i;
                miTheme2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        try {
                            Properties property = new Properties();
                            try {
                                property.load(new FileInputStream(new File(StorageManager.getInstance().getSettingsDirectory(), "lookandfeel.properties")));
                            } catch (Exception ex) {
                                Logger.getLogger(com.logical.allhit.setting.Setting.class.getName()).log(Level.WARNING, ex.getMessage());
                            }
                            UIManager.setLookAndFeel(lfinfo[j].getClassName());
                            SwingUtilities.updateComponentTreeUI(Kernel.getInstance().getFrame());
                            property.setProperty("app.lookandfeel", lfinfo[j].getName());
                            try {
                                FileOutputStream fos = new FileOutputStream(new File(StorageManager.getInstance().getSettingsDirectory(), "lookandfeel.properties"), false);
                                property.store(fos, "Look And Feel Setting");
                            } catch (Exception e) {
                                Logger.getLogger(com.logical.allhit.setting.Setting.class.getName()).log(Level.WARNING, e.getMessage(), e);
                            }
//                            userinfo.con.prepareStatement("Update displaysettings Set ThemeName='" + lfinfo[j].getName() + "' where User_Id = " + userinfo.ID).executeUpdate();
                            
                        } catch (Exception ex) {
                            Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, "mainWindow not fount...", ex);
                        }
                    }
                });
                menuTheme.add(miTheme2);
            }
        } catch (Exception ex) {
            Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, "mainWindow not fount...", ex);
        }
        return menuTheme;
    }
    
    public JFrame getFram() {
        return mainWindow;
    }
}
